package com.example.livedata.model;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel;

import com.example.livedata.entity.UserInfo;
import com.example.livedata.repository.UserRepository;

/**
 * 负者数据传递，以及保存对象，保持和activity生命周期同步
 */
public class UserModel extends ViewModel {


    private LiveData<UserInfo> user;

    private UserRepository userRepository;


    public UserModel(UserRepository repository) {
        this.userRepository = repository;


    }

    public void init(String userId) {
        if (this.user != null) {
            // ViewModel 是由 Fragment 创建的
            // 所以我们知道 userId 不会改变
            return;
        }
        user = userRepository.getUser(userId);
    }


    public LiveData<UserInfo> getUser() {
        return this.user;
    }

}
